$(function(){
    checkRemember();
    $("#vcodeImage").attr("src",basePath+"login/getVcode");
    $("#vcodeImage").click(function(){
        var src = basePath+"login/getVcode";
        $(this).attr("src",src).fadeIn();
    })

    $("#logSubmitButton").click(function(){
        doLogin();
    })

    /* 登录框enter事件 */
    $(document).keypress(function(e){
        if(e.keyCode == "13"){
            if($("#userName").is(":focus")){
                $("#password").focus();
            }else if($("#password").is(":focus")){
                // $("#vcode").focus();
                doLogin();
            }/*else if($("#vcode").is(":focus")){
                doLogin();
            }*/
        }
    });
});


//登录校验
function checkForm(){
    var userName=$.trim($("#userName").val());
    var password=$.trim($("#password").val());
    // var vcode=$.trim($("#vcode").val());
    if(userName.length==0){
        $("#loginwarning").empty().html("<span style='color:red'>请填写登录账号!</span>");
        return false;
    }
    if(password.length==0){
        $("#loginwarning").empty().html("<span style='color:red'>请输入登录密码!</span>");
        return false;
    }
    /*if(vcode.length==0){
        $("#loginwarning").empty().html("<span style='color:red'>请输入图片验证码!</span>");
        return false;
    }*/
    if(password.length<6||password.length>15){
        $("#loginwarning").empty().html("<span style='color:red'>账号或密码错误!</span>");
        return false;
    }
    /*if(vcode.length!=4){
        $("#loginwarning").empty().html("<span style='color:red'>请输入4位图片验证码!</span>");
        return false;
    }*/
    return true;
}

function checkRemember(){

}

function doLogin(){
    if(!checkForm())return;
    var passwordOld=$("#password").val();
    $("#password").val($.md5($("#password").val()).toUpperCase());
    var userName=$.trim($("#userName").val());
    var password=$.trim($("#password").val());
    var remember=$("#remember").attr("checked");
    $.ajax({
        url: basePath+"loginIn",
        type: "post",
        dataType: "json",
        data:$("#loginForm").serialize(),
        success: function (back) {
            if(!back){
                alert("登陆失败");
                $("#password").val(passwordOld);
                // $("#vcodeImage").attr("src",basePath+"login/getVcode");
                return;
            }
            if(!back.result){
                $("#loginwarning").empty().html("<span style='color:red'>"+back.message+"</span>");
                //$("#vcodeImage").trigger("click");
                $("#password").val(passwordOld);
                // $("#vcodeImage").attr("src",basePath+"login/getVcode");
                return;
            }
            $("#password").val(passwordOld);
            //登录成功跳转至首页
            window.location = basePath+"index";
        }
    });
}